//
//  YBChangePhoneViewController.m
//  ZhongWangYuanApp
//
//  Created by apple on 2019/1/29.
//  Copyright © 2019年 apple. All rights reserved.
//

#import "YBChangePhoneViewController.h"
#import "ZJAdditions.h"
#import "UIButton+countDown.h"
@interface YBChangePhoneViewController ()<UITextFieldDelegate>

/** 手机号码 */
@property (nonatomic, strong) UITextField *phoneNumber;
/** 验证码 */
@property (nonatomic, strong) UITextField *verification;
/** 获取验证码 */
@property (nonatomic, strong) UIButton *verificationButton;
/** 注册 */
@property (nonatomic, strong) UIButton *userRegist;
@end

@implementation YBChangePhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"修改手机号";
    [self setupUI];
//    self.phoneNumber.text = self.phoneString;
}
- (void)setupUI{
    [self.view addSubview:self.phoneNumber];
    [self.view addSubview:self.verification];
    [self.view addSubview:self.verificationButton];
    [self.view addSubview:self.userRegist];
    
    //手机号提示
    UILabel *phoneLabel = [UILabel new];
    phoneLabel.text = @"手机号 :";
    phoneLabel.textColor = [UIColor textBlackColor];
    phoneLabel.font = [UIFont titleTextFont];
    //验证码提示
    UILabel *VLabel = [UILabel new];
    VLabel.text = @"验证码 :";
    VLabel.textColor = [UIColor textBlackColor];
    VLabel.font = [UIFont titleTextFont];
    [self.view addSubview:phoneLabel];
    [self.view addSubview:VLabel];
    
    
    phoneLabel.sd_layout
    .topSpaceToView(self.navigView,25 * [UIScreen zj_screenHScale])
    .leftSpaceToView(self.view, 27 * [UIScreen zj_screenWScale])
    .rightSpaceToView(self.view, 27 * [UIScreen zj_screenWScale])
    .heightIs([NSString heightWithTextFontSize:[UIFont titleTextFontSize]]);
    
    self.verificationButton.sd_layout
    .topSpaceToView(phoneLabel, 11 * [UIScreen zj_screenHScale])
    .rightEqualToView(phoneLabel)
    .widthIs(100 * [UIScreen zj_screenWScale])
    .heightIs(44 * [UIScreen zj_screenHScale]);
    
    self.phoneNumber.sd_layout
    .topSpaceToView(phoneLabel, 11 * [UIScreen zj_screenHScale])
    .leftEqualToView(phoneLabel)
    .rightSpaceToView(self.verificationButton, 12 * [UIScreen zj_screenWScale])
    .heightIs(44 * [UIScreen zj_screenHScale]);
    
    
    
    VLabel.sd_layout
    .topSpaceToView(self.phoneNumber, 23 * [UIScreen zj_screenHScale])
    .leftEqualToView(self.phoneNumber)
    .rightEqualToView(self.phoneNumber)
    .heightIs([NSString heightWithTextFontSize:[UIFont titleTextFontSize]]);
    
    self.verification.sd_layout
    .topSpaceToView(VLabel, 11 * [UIScreen zj_screenHScale])
    .leftSpaceToView(self.view, 27 * [UIScreen zj_screenWScale])
    .rightSpaceToView(self.view, 27 * [UIScreen zj_screenWScale])
    .heightRatioToView(self.phoneNumber, 1);
    
    self.userRegist.sd_layout
    .bottomSpaceToView(self.view, ItemHeight * 75)
    .leftSpaceToView(self.view, 27 * [UIScreen zj_screenWScale])
    .rightSpaceToView(self.view, 27 * [UIScreen zj_screenWScale])
    .heightRatioToView(self.phoneNumber, 1);
}





#pragma mark - 点击事件
/** 发送验证码 */
- (void)action_sendVerification{
    [self.phoneNumber resignFirstResponder];
    [self.verification resignFirstResponder];
    if (self.phoneNumber.text.length == 0) {
        [YJProgressHUD showMsgWithoutView:@"请输入的手机号码"];
        return;
    }
    if (![CManager zj_validateMobile:self.phoneNumber.text]) {
        [YJProgressHUD showMsgWithoutView:@"请输入正确的手机号码"];
        return;
    }
    [YJProgressHUD showMessage:@"正在发送..." duration:10 view:self.view];
    [YBRequestManager request_sendVerifySMSCodeWithPhone:self.phoneNumber.text successBlock:^(id object) {
        [YJProgressHUD hide];
        if ([object[@"code"] integerValue] == 1) {
            [self.verificationButton startWithTime:60 title:@"重新获取" countDownTitle:@"重新获取" mainColor:[UIColor textBlackColor] countColor:[UIColor textBlackColor]];
        }
        else{
            [YJProgressHUD showMsgWithoutView:@"获取失败"];
        }
    } failBlock:^(id object) {
        [YJProgressHUD showMsgWithoutView:@"获取失败"];
    }];
    
}



/** 注册 */
- (void)action_userRegist{
    [self.view endEditing:YES];
    
    if (self.phoneNumber.text.length == 0) {
        [YJProgressHUD showMsgWithoutView:@"请输入的手机号码"];
        return;
    }
    if (![CManager zj_validateMobile:self.phoneNumber.text]) {
        [YJProgressHUD showMsgWithoutView:@"请输入正确的手机号码"];
        return;
    }
    if (self.verification.text.length == 0) {
        [YJProgressHUD showMsgWithoutView:@"请输入的短信验证码"];
        return;
    }
    //输入的信息都正确
    [YJProgressHUD showMessage:@"修改中..." duration:30 view:self.view];
    
    [YBRequestManager request_changePhoneNumberWithPhoneNumber:self.phoneNumber.text inputsmscode:self.verification.text successBlock:^(id object) {
        [YJProgressHUD hide];
        if([object[@"code"] integerValue] == 1) {
            if ([object[@"data"][@"result"] integerValue] == 1) {
                [YJProgressHUD showMessage:@"修改成功" inView:self.view compeleteBlock:^{
                    [YBUserInfoModel ybChangeUserInfoWithKey:@"cellphone" value:self.phoneNumber.text];
                    [self.navigationController popViewControllerAnimated:YES];
                }];
                
            }
            else{
                [YJProgressHUD showMessage:@"修改失败" inView:self.view];
            }
        }
        else{
            [YJProgressHUD showMessage:@"修改失败" inView:self.view];
        }
    } failBlock:^(id object) {
        [YJProgressHUD showMessage:@"修改失败" inView:self.view];
    }];
    
}


#pragma mark - UITextFieldDelegate 协议方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (textField == self.phoneNumber) {
        if (toBeString.length >= 11 && range.length!=1){
            textField.text = [toBeString substringToIndex:11];
            return NO;
            
        }
    }else if (textField == self.verification) {
        if (toBeString.length >= 6 && range.length!=1){
            textField.text = [toBeString substringToIndex:6];
            return NO;
            
        }
    }
    
    return YES;
}

#pragma mark - Setter
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];//取消第一响应者
    
    return YES;
}

#pragma mark - Getters

- (UITextField *)phoneNumber{
    if (_phoneNumber == nil){
        _phoneNumber = [[UITextField alloc]init];
        _phoneNumber.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        _phoneNumber.placeholder = @"请输入手机号";
        _phoneNumber.textColor = [UIColor textBlackColor];
        _phoneNumber.font = [UIFont titleTextFont];
        [_phoneNumber setTextFieldLeftforWidth:14 * [UIScreen zj_screenWScale]];
        [_phoneNumber setTextFieldRightforWidth:14 * [UIScreen zj_screenWScale]];
        _phoneNumber.delegate = self;
        _phoneNumber.layer.borderColor = [UIColor zj_colorWithHexString:@"ededed"].CGColor;
        _phoneNumber.layer.borderWidth = 1.0;
        _phoneNumber.sd_cornerRadius = @(4 * [UIScreen zj_screenHScale]);
        _phoneNumber.returnKeyType = UIKeyboardTypeTwitter;
        
    }
    return _phoneNumber;
}

- (UITextField *)verification{
    if (_verification  == nil){
        _verification = [[UITextField alloc]init];
        _verification.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        _verification.placeholder = @"请输入手机验证码";
        _verification.textColor = [UIColor textBlackColor];
        _verification.font = [UIFont titleTextFont];
        [_verification setTextFieldLeftforWidth:14 * [UIScreen zj_screenWScale]];
        _verification.delegate = self;
        [_verification setTextFieldRightforWidth:14 * [UIScreen zj_screenWScale]];
        _verification.layer.borderColor = [UIColor zj_colorWithHexString:@"ededed"].CGColor;
        _verification.layer.borderWidth = 1.0;
        _verification.sd_cornerRadius = @(4 * [UIScreen zj_screenHScale]);
        _verification.returnKeyType = UIKeyboardTypeTwitter;
        
    }
    return _verification;
}

- (UIButton *)verificationButton{
    if (_verificationButton == nil){
        _verificationButton = [[UIButton alloc]init];
        [_verificationButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_verificationButton setTitleColor:[UIColor textBlackColor] forState:UIControlStateNormal];
        _verificationButton.titleLabel.font = [UIFont systemFontOfSize:15 * [UIScreen zj_textFontScale]];
        _verificationButton.layer.cornerRadius = 4 * [UIScreen zj_screenHScale];
        _verificationButton.layer.borderColor = [UIColor zj_colorWithHexString:@"ededed"].CGColor;
        _verificationButton.layer.borderWidth = 1.0;
        [_verificationButton addTarget:self action:@selector(action_sendVerification) forControlEvents:UIControlEventTouchUpInside];
    }
    return _verificationButton;
}
- (UIButton *)userRegist{
    if (_userRegist == nil){
        _userRegist = [[UIButton alloc]init];
        [_userRegist setTitle:@"保存" forState:UIControlStateNormal];
        [_userRegist setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _userRegist.backgroundColor = [UIColor themeColor];
        _userRegist.titleLabel.font = [UIFont titleTextFont];
        _userRegist.layer.cornerRadius = 4 * [UIScreen zj_screenHScale];
        [_userRegist addTarget:self action:@selector(action_userRegist) forControlEvents:UIControlEventTouchUpInside];
    }
    return _userRegist;
}


@end
